package kk.web.servlet;

import kk.domain.PageBean;
import kk.domain.User;
import kk.service.UserService;
import kk.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/findUserByPageServlet")
public class FindUserByPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("UTF-8");
        //接受参数
        String currentPage = request.getParameter("currentPage");
        String rows = request.getParameter("rows");
        //如果客户端没有输入当前页面和每页显示行数，则默认当前是第一页，每页显示五条记录
        if(currentPage == null || "".equals(currentPage)){
            currentPage = "1";
        }
        if(rows == null || "".equals(rows)){
            rows = "5";
        }
        //接受条件查询传递的参数
        Map<String, String[]> condition = request.getParameterMap();
        //调用service
        UserService userService=new UserServiceImpl();
        PageBean<User> pb=userService.findUserByPage(currentPage,rows,condition);
        //将PageBean存入request域中
        request.setAttribute("pb",pb);
        //将condition存入request域中
        request.setAttribute("condition",condition);
        //转发
        request.getRequestDispatcher("/list.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
